#pragma once

#include <iostream>
#include <vector>
using namespace std;

namespace zzh
{
	template<typename T>
	class Less
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template<typename T>
	class Greater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}
	};


	template<typename T, typename Container = vector<T>, typename Compare = Less<T>>
	class priority_queue
	{
	public:
		priority_queue() = default;

		size_t size() const
		{
			return _con.size();
		}

		bool empty() const
		{
			return _con.empty();
		}

		const T& top() const
		{
			return _con[0];
		}

		void push(const T& value)
		{
			_con.push_back(value);
			AdjustUp(_con.size() - 1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			AdjustDown(0);
		}

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push(*first);
				++first;
			}
		}

	private:
		void AdjustUp(int child)
		{
			while (child > 0)
			{
				int parents = (child - 1) / 2;
				if (_com(_con[parents], _con[child]))
				{
					swap(_con[parents], _con[child]);
					child = parents;
				}
				else
				{
					break;
				}
			}
		}

		void AdjustDown(int parents)
		{
			int child = parents * 2 + 1;
			while (child < size())
			{
				if (child + 1 < size() && _com(_con[child], _con[child + 1]))
				{
					++child;
				}

				if (_com(_con[parents], _con[child]))
				{
					swap(_con[parents], _con[child]);
					parents = child;
					child = parents * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}

	private:
		Container _con;
		Compare _com;
	};

}